// 04jyhs/shoppingMallAdmin/GoodsList/GoodsList.js
var co = require('../../../resource/js/common.js')
var app = getApp()
var appData = app.Data
var _this, count = 0, _data, _isCompile = false;//编辑还是新建

Page({
   data: {
      // 分类商品数据
      goodsList: [],
      // 商品分类
      goodsCateList: '',
      // 屏幕高度
      win_h: '',
      // 分类点击id
      idx: 0,
      // 排序
      toExchange: true,
      //调换的下标
      exchange: -1,
      // 置顶
      upTop: true,
      shopInfo: ''
   },

   //进入页面加载
   onLoad: function () {
      _this = this
      _data = this.data
      this.setData({ win_h: wx.getSystemInfoSync().windowHeight })
      this.getGoodsCateData()
      _this.setData({
         shopInfo: appData.shopInfo
      })
   },
   //隐藏页面的时候清空数据
   onHide: function () {
      app.Data.goodsList = ''
   },

   //返回进入自动更新数据
   onShow: function () {
      if (!co.empty(app.Data.goodsCateList)) {
         _this.setData({ goodsCateList: app.Data.goodsCateList })
         app.Data.goodsCateList = ''
      } else if (app.Data.goodsCateList == '0') {
         this.setData({ goodsCateList: '' })
      }
      if (!co.empty(app.Data.goodsList)) {
            // 编辑商品
         if (_data.goodsList[_data.idx] == undefined){
             return
         }else{
            var goodsList = _data.goodsList
            var i = co.getIndexInList(app.Data.goodsList, _data.goodsList[_data.idx], ['Products_ID'])
            if (_data.idx == app.Data.pick_i) {
               goodsList[_data.idx][i] = app.Data.goodsList
            } else {
               goodsList[_data.idx].splice(i, 1);
               if (!co.empty(goodsList[app.Data.pick_i])) {
                  goodsList[app.Data.pick_i].push(app.Data.goodsList)
               }
            }
            _this.setData({ goodsList: goodsList })
            app.Data.goodsList = ''
         }
      }
   },

   //获取商家自定义分类 
   getGoodsCateData: function () {
      co.getMealApi('jyhs/Prdmg/getBizCategoryList', {
         Biz_ID: appData.shopInfo.Biz_ID
      }, function (goodsData) {
         if (goodsData.code == 0) {
            _this.setData({ goodsCateList: goodsData.data })
            _this.getGoodsList(goodsData.data[0].Category_ID)
         }
      })
   },

   //获取商品
   getGoodsList: function (cateId, _function) {
      co.getMealApi('jyhs/Prdmg/getList', {
         Biz_ID: appData.shopInfo.Biz_ID,
         Products_BizCategory: cateId,
         page:1
      }, function (res) {
         if (co.empty(_function)) {
            if (res.code == 0) {
               console.log("【获取分类商品数据】===", res.data)
               var map = res.data, pt = _data.goodsList, p = []
               for (var k in map) {
                  p.push(map[k])
               }
               pt[_data.idx] = p
               _this.setData({ goodsList: pt })
            } else if (res.code == 29300) {
               _this.setData({ goodsList: res.data })
            } else {
               co.toastAllText(res.message)
            }
         } else {
            if (res.code == 0 || res.code == 29300) {
               var cateFoodList = res.data
               var c = 'goodsList[' + app.Data.pick_i + ']'
               _this.setData({ [c]: cateFoodList })
            }
         }
      })
   },
   //商品分类点击事件
   cateClick: function (e) {
      var index = Number(e.currentTarget.id)
      // 获取商品分类数据
      var clickID = _data.goodsCateList[index].Category_ID
      if (co.empty(_data.goodsList[index])) {
         this.getGoodsList(clickID)
      }
      this.setData({ idx: index })
   },

   //编辑商品
   editProduct: function (e) {
         var obj = JSON.stringify(_data.goodsList[_data.idx][e.currentTarget.id])
         wx.navigateTo({
            url: '/04jyhs/shoppingMallAdmin/publishGoods/publishGoods?obj=' + obj + '&idx=' + _data.idx
         })
   },
   //删除或上下架
   putAndEditProduct: function (e) {
      var i = e.currentTarget.id
      var index = e.currentTarget.dataset.index
         var param = {
            Biz_ID: appData.shopInfo.Biz_ID,
            Products_ID: _data.goodsList[_data.idx][i].Products_ID
         }
         if (index == 1) {
            if (_data.goodsList[_data.idx][i].Products_Status == 3) {
               param.Products_Status = 1
            } else {
               param.Products_Status = 3
            }
            _this.deleteProducts(param)
         }
         if (index == 2) {
            co.confirm('', '确认删除吗？', '取消', '删除', function (res) {
               if (res) {
                  param.Is_Delete = -1
                  _this.deleteProducts(param)
               }
            })
         }
   },

   //删除商品
   deleteProducts: function (param) {
      co.getMealApi('jyhs/Prdmg/edit', param, function (res) {
         if (res.code == 0) {
            _this.getGoodsList(_data.goodsCateList[_data.idx].Category_ID)
         } else {
            co.toastAllText(res.message)
         }
      })
   },
})